package com.supermali.xml;

import com.supermali.creater.MapCreater;
import com.supermali.entity.MapImageAbstract;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class EntityDom {
    // 第x个方块开始
    private float x;
    // 第y个方块开始
    private float y;
    // x方向多少个
    private int cx;
    // y方向多少个
    private int cy;
    // 对应的实体类路径
    private String entityPath;
    // shape 形状
    private String shape;
    // 排序
    private int order;
    //宽
    private int width;
    // 高
    private int height;

    public enum DrawShape{
        NORMAL("normal"),
        CIRCLE("circle")
        ,RECTANGLE("rectangle")
        ,STAIRS("stairs")
        ,REVERSE_STAIRS("reverse_stairs");
        String desc;

        DrawShape(String desc) {
            this.desc = desc;
        }

        public String getDesc() {
            return desc;
        }
    }

    public DrawShape getShape() {
        if(shape==null) {
            return DrawShape.NORMAL;
        }else {
            DrawShape[] values = DrawShape.values();
            for(DrawShape drawShape: values){
                String desc = drawShape.desc;
                if(desc.equalsIgnoreCase(shape)){
                    return drawShape;
                }
            }
            return DrawShape.NORMAL;
        }
    }

    public Object createInstance(double x,double y,MapCreater mapCreater){
        try {
            Class<?> forName = Class.forName(entityPath);
            // 获取到非装饰器的类
            Constructor<?> constructor = forName.getDeclaredConstructor(Double.class,Double.class,MapCreater.class);
            Object newInstance = constructor.newInstance(x, y,mapCreater);
            if(newInstance instanceof MapImageAbstract){
                ((MapImageAbstract) newInstance).setOrder(order);
                ((MapImageAbstract) newInstance).setWidth(width);
                ((MapImageAbstract) newInstance).setHeight(height);
            }
            return newInstance;
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Class getEntityPathClass(){
        try {
            Class<?> forName = Class.forName(entityPath);
            return forName;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String toString() {
        return "EntityDom{" +
                "x=" + x +
                ", y=" + y +
                ", cx=" + cx +
                ", cy=" + cy +
                ", entityPath='" + entityPath + '\'' +
                ", shape='" + shape + '\'' +
                ", order=" + order +
                '}';
    }

    /////////////
    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public int getCx() {
        return cx;
    }

    public void setCx(int cx) {
        this.cx = cx;
    }

    public int getCy() {
        return cy;
    }

    public void setCy(int cy) {
        this.cy = cy;
    }

    public String getEntityPath() {
        return entityPath;
    }

    public void setEntityPath(String entityPath) {
        this.entityPath = entityPath;
    }

    public void setShape(String shape) {
        this.shape = shape;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}
